<?php

namespace App\V3\Api\Models;

use App\V3\Api\Models\MaterielContentModel;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class MaterielModel extends Model
{
    use HasFactory;

    protected $table = 'materiel';
    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    protected $dates = [
        'purchase_time',
        'pay_time'
    ];

    public function materielContent()
    {
        return SELF::hasMany(MaterielContentModel::class, 'materiel_id', 'id');
    }

    static public function getMaterielId($condition, $userId)
    {
        return SELF::select(
            'id as materiel_id'
        )
            ->from('materiel as a')
            ->where($condition)
            ->where($userId)->get()->toArray();
    }
}
